<?php
// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

/**
* @package Joomla
* @subpackage Websites
*/
class HTML_district {

	function showdistrict( &$rows, &$pageNav, $search, $option, &$lists ) {
		global $my, $database;

		mosCommonHTML::loadOverlib();
		$city_id = mosGetParam($_REQUEST, 'city_id', 0);
		?>
		<form action="index2.php" method="post" name="adminForm">
		<table class="adminheading">
		<tr>
			<th>
			Quản trị quận / huyện
			</th>
			<td>
			Tìm:
			</td>
			<td>
			<input type="text" name="search" value="<?php echo htmlspecialchars( $search );?>" class="inputbox" onChange="document.adminForm.submit();" />
			</td>
			<td width="right">
			<select name="lists" style="width:150px; height:19px;" onchange="window.open(this.options[this.selectedIndex].value,'_self');" >
			<?php
			if (!$city_id || $city_id==0){
				echo "<option value='index2.php?option=com_district' selected>Lựa chọn theo hãng</option>";
				for ($i=0; $i<count($lists); $i++){
					echo "<option value='index2.php?option=com_district&city_id=".$lists[$i]->id."'>".$lists[$i]->title."</option>";
				}
			}else{
				$sql = "SELECT * FROM #__city WHERE id=".$city_id;
				$database->setQuery($sql);
				$database->loadObject($selected);
				echo "<option value='index2.php?option=com_district&city_id=".$selected->id."' selected>".$selected->title."</option>";
				$sql = "SELECT * FROM #__city WHERE id!=".$city_id." ORDER BY title ASC";
				$database->setQuery($sql);
				$list_select = $database->loadObjectList();
				for ($i=0; $i<count($list_select); $i++){
					echo "<option value='index2.php?option=com_district&city_id=".$list_select[$i]->id."'>".$list_select[$i]->title."</option>";
				}
				echo "<option value='index2.php?option=com_district&city_id=0'>Xem tất cả các nhóm</option>";
			}			
			?>
			</select>
			</td>
		</tr>
		</table>

		<table class="adminlist">
		<tr>
			<th width="20">
			#
			</th>
			<th width="20" class="title">
			<input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count($rows); ?>);" />
			</th>
			<th class="title">
			Tên quận / huyện
			</th>		
			<th width="5%" class="title" nowrap="nowrap">
			Trạng thái
			</th>
			<th width="2%" class="title" nowrap="nowrap"></th>
			<th colspan="2" nowrap="nowrap" width="5%">
			Sắp xếp
			</th>
			<th width="2%" class="title" nowrap="nowrap"></th>
			<th align="left" class="title">
			Thuộc tỉnh
			</th>			
		</tr>
		<?php
			$list_rows = new mosDistrict($list_rows);
			//echo $v = $list_rows->listRows(0,"");
		?>
		<?php
		$k = 0;
		for ($i=0, $n=count($rows); $i < $n; $i++) {
			$row = $rows[$i];
			mosMakeHtmlSafe($row);
			$link 	= 'index2.php?option=com_district&task=editA&hidemainmenu=1&id='. $row->id;

			$img 	= $row->published ? 'tick.png' : 'publish_x.png';
			$task 	= $row->published ? 'unpublish' : 'publish';
			$alt 	= $row->published ? 'published' : 'Unpublish';

			$checked 	= mosCommonHTML::CheckedOutProcessing( $row, $i );

			?>
			<tr class="<?php echo "row$k"; ?>">
				<td>
				<?php echo $pageNav->rowNumber( $i ); ?>
				</td>
				<td>
					<?php echo $checked; ?>
				</td>
				<td>				
					<a href="<?php echo $link; ?>" title="Cập nhật nhóm rao vặt">
					<?php echo $row->title; ?>
					</a>										
				</td>				
				<td align="center">
					<a href="javascript: void(0);" onClick="return listItemTask('cb<?php echo $i;?>','<?php echo $task;?>')">
					<img src="images/<?php echo $img;?>" width="12" height="12" border="0" alt="<?php echo $alt; ?>" />
					</a>
				</td>
				<td></td>
				<td>
				<?php echo $pageNav->orderUpIcon( $i, ( $row->catid == @$rows[$i-1]->catid ) ); ?>
				</td>
				<td>
				<?php echo $pageNav->orderDownIcon( $i, $n, ( $row->catid == @$rows[$i+1]->catid ) ); ?>
				</td>
				<td></td>
				<td>
				<?php
				$sql = "select id,title from #__city where id=".$row->city_id;
				$database->setQuery($sql);
				$database->loadObject($rowgroup);
				$linkG = "index2.php?option=com_district&city_id=".$rowgroup->id;
				?>
				<a href="<?php echo $linkG; ?>" title="Edit Category">
				<?php echo $rowgroup->title; ?>
				</a>
				</td>				
			</tr>
			<?php
			$k = 1 - $k;
		}
		?>
		</table>
		<?php echo $pageNav->getListFooter(); ?>

		<input type="hidden" name="option" value="<?php echo $option; ?>" />
		<input type="hidden" name="task" value="" />
		<input type="hidden" name="boxchecked" value="0" />
		<input type="hidden" name="hidemainmenu" value="0">
		<input type="hidden" name="<?php echo josSpoofValue(); ?>" value="1" />
		</form>
		<?php
	}

	/**
	* Writes the edit form for new and existing record
	*
	* A new record is defined when <var>$row</var> is passed with the <var>id</var>
	* property set to 0.
	* @param mosWebsite The Website object
	* @param array An array of select lists
	* @param object Parameters
	* @param string The option
	*/
	function editdistrict( &$row, $option, &$lists ) {
		mosMakeHtmlSafe( $row, ENT_QUOTES, 'content' );
		global $database, $mosConfig_live_site;
		mosCommonHTML::loadOverlib();
		$uploaddir = $mosConfig_live_site."/administrator/components/com_district/uploads/";
		$id = $_GET['id'];
		if ($row->image == '') {
			$row->image = 'blank.png';
		}

		$tabs = new mosTabs(0);

		mosMakeHtmlSafe( $row, ENT_QUOTES, 'misc' );
		$rowid = intval($row->id);				
		?>
		<script language="javascript" type="text/javascript">
		function submitbutton(pressbutton) {
			var form = document.adminForm;
			if (pressbutton == 'cancel') {
				submitform( pressbutton );
				return;
			}
			else if (form.city_id.value == ""){
				alert( "Vui lòng chọn tỉnh / thành phố !" );
				form.city_id.focus();
			}						
			else if (form.title.value == ""){
				alert( "Tên quận / huyện vui lòng không để trống !" );
				form.title.focus();
			}											
			else {
				submitform( pressbutton );
			}
		}
		</script>
		<form action="index2.php" enctype="multipart/form-data" method="post" name="adminForm" id="adminForm">
		<table class="adminheading">		
		<tr>
			<th>
			Quản trị quận / huyện :
			<small>
			<?php echo $row->id ? 'Edit' : 'New';?>
			</small>
			</th>
		</tr>		
		<tr>
			<td width="50%" valign="top">
				<table class="adminform">				
				<tr>
					<td valign="top" align="right" colspan="2"><B>Thông tin quận / huyện</B></td>
				</tr>
				<tr>
					<td width="18%" align="right">
					Chọn tỉnh / thành phố :
					</td>
					<td width="40%">
					<select class="inputbox" name="city_id" style="width:250px;">
						<?php
						if ($rowid==0){
							echo "<option value=''>--- Chọn tỉnh / thành phố ---</option>";
							$sql = "SELECT id,title FROM #__city WHERE published=1 ORDER BY title ASC, ordering ASC";
							$database->setQuery($sql);
							$listCity = $database->loadObjectList();
							for ($i=0; $i<count($listCity); $i++){
								echo "<option value='".$listCity[$i]->id."'>".$listCity[$i]->title."</option>";
							}
						}
						else{
							$sql = "SELECT id,title FROM #__city WHERE published=1 AND id=".$row->city_id;
							$database->setQuery($sql);
							$database->loadObject($cityted);
							echo "<option value='".$cityted->id."' selected>".$cityted->title."</option>";
							$sql = "SELECT id,title FROM #__city WHERE published=1 AND id!=".$row->city_id." ORDER BY title ASC, ordering ASC";
							$database->setQuery($sql);
							$listCity = $database->loadObjectList();
							for ($i=0; $i<count($listCity); $i++){
								echo "<option value='".$listCity[$i]->id."'>".$listCity[$i]->title."</option>";
							}
						}
						?>
					</select>
					</td>
				</tr>					
				<tr>
					<td width="20%" align="right">
					Tên quận / huyện :
					</td>
					<td width="80%">
					<input class="text_district" type="text" name="title" size="45" maxlength="200" value="<?php echo $row->title;?>" />					
					</td>
				</tr>
				<tr>
					<td width="20%" align="right">
					Bí danh :
					</td>
					<td width="80%">
					<input class="text_district" type="text" name="title_alias" size="45" maxlength="200" value="<?php echo $row->title_alias;?>" disabled="disabled" />					
					</td>
				</tr>																	
				<tr>
					<td valign="top" align="right">
					Trạng thái:
					</td>
					<td>					
					<input type="radio" name="published" value="0" <?php if($row->published==0) echo "checked='checked'";?> />	Khóa
					<input type="radio" name="published" value="1" <?php if($row->published==1) echo "checked='checked'";?> />	Hiển thị					
					</td>
				</tr>
				<tr>
					<td valign="top" align="right">
					Thứ tự:
					</td>
					<td>
					<input type="text" name="ordering" size="10" maxlength="100" value="<?php echo $row->ordering;?>" />
					</td>
				</tr>				
				<tr>
					<td colspan="2">&nbsp;</td>
				</tr>														
		</table>

		<input type="hidden" name="id" value="<?php echo $row->id; ?>" />
		<input type="hidden" name="option" value="<?php echo $option;?>" />
		<input type="hidden" name="task" value="" />
		</form>
		<?php
	}
}
?>